<?php

	require_once("../logic/connection.php");		
	
	
	
	if(isset($_REQUEST['submit']))
	{
			$title=$_REQUEST['title'];
			$author=$_REQUEST['author'];
			$publisher=$_REQUEST['publisher'];
			$price=$_REQUEST['price'];
			$isbn=$_REQUEST['isbn'];
			//echo $isbn;
			$res=$con->prepare('select book_id from book_info where isbn=?');
			$res->bind_param("s",$isbn);			
			$res->execute();
			$res->bind_result($bid);
			$res->fetch();
			if($bid!="")
				echo "This book already exsit.";
			else
			{
				//echo "found";
				if($_FILES['image']['name']!="" && $_FILES['file']['name']!="")
				{

					//echo "found1";
					$image=$_FILES['image']['name'];
					$filepath = md5($isbn).$_FILES['file']['name'];
					$path1="../books";
					$path2="../img";					
					$fileupload=$path1."/".$filepath;
					$imageupload=$path2."/".$image;
					if(move_uploaded_file($_FILES["image"]["tmp_name"],$imageupload))
					{
						//echo "found2";
						if(move_uploaded_file($_FILES["file"]["tmp_name"], $fileupload))
						{
							$con=getConnection();
							$str=$con->prepare("insert into book_info(title,author,publisher,price,isbn,image,file_path) values(?,?,?,?,?,?,?)");
							$str->bind_param('sssisss',$title,$author,$publisher,$price,$isbn,$image,$filepath);
							$str->execute();
							 header('Location: success.html');
						}
						else
						{
							echo "error in file upload";
						}
					}
				
				}
			}
			
		
	}
	
	
	
	

?>
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

	<title></title>
	<meta name="description" content="">
	<meta name="author" content="">

	<meta name="viewport" content="width=device-width">

	<link rel="stylesheet" href="../css/bootstrap.min.css">
	<style>
	body {
	  padding-top: 60px;
	  padding-bottom: 40px;
	}
	
	/*validation error styles
------------------------------------------------------------------*/
#msg {
	display:none; 
	position:absolute; 
	z-index:200; 
	background:url(../images/msg_arrow.png) left center no-repeat; 
	padding-left:7px}
#msgcontent {display:block; background:#ffe5e5; border:2px solid #ff8b7b; border-left:none; padding:5px; min-width:150px; max-width:250px}

	</style>
	<link rel="stylesheet" href="../css/bootstrap-responsive.min.css">
	<link rel="stylesheet" href="../css/style.css">
	
	<script src="../js/libs/modernizr-2.5.3-respond-1.1.0.min.js"></script>
    <script src="../js/libs/jquery-1.7.2.min.js"></script>
    <script src="registration.js"></script>
    
</head>
<body>
<!--[if lt IE 7]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->

    <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="index.php">E-library-Admin Panel</a>
          <div class="nav-collapse">
            <ul class="nav">
              <li><a href="addbook.php">Add Book</a></li>
				  <li><a href="logout.php">Log Out</a></li>
			 
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container">
<form method="post" class="well" enctype="multipart/form-data" onSubmit="return validate(this);">

	<h2>Add Book</h2>
    <hr>

	<div class="span4">
    	<p>
            <label>Title:</label>
            <input type="text"  name="title" id="title" />
        </p>
        
        <p>   
            <label>Price:</label>
            <input type="text" name="price" id="price" />
         </p>
         
          <p>
            <label>File:</label>
            <input type="file" name="file" id="file" />
         </p>
         
         <p><input type="submit" name="submit" class="btn btn-primary btn-large" value="Add Book" /></p>
        
    </div>
    <div class="span4">
    	<p>
            <label>Author:</label>
            <input type="text" name="author" id="author" />
        </p>
        
        <p>   
            <label>ISBN:</label>
            <input type="text" name="isbn" id="isbn" />
         </p>
         
    </div>
    
    <div class="span3">
    	<p>   
            <label>Publisher:</label>
            <input type="text" name="publisher" id="publisher" />
         </p>
         
         <p>
            <label>Book Image:</label>
            <input type="file" name="image" id="image" />
         </p>
         
         
         
    </div>
	
<div class="clearfix"></div>

</form>

</div>
</body>
</html>